<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/18
 * Time: 下午9:55
 */

class ModPlatformGameMap extends Model {

    private $table = 'tblPlatformGameMap';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbAuth;
    }

    public function select($params = array()){
        $where  = ' WHERE 1 ';
        $args   = array();

        if(!empty($params['platform'])){
            $where .= ' AND a.PlatformID=:PlatformID ';
            $args[':PlatformID'] = $params['platform'];
        }

        if(!empty($params['game'])){
            $where .= ' AND a.GameID=:GameID ';
            $args[':GameID'] = $params['game'];
        }

        $sql = "SELECT a.GameID,a.PlatformID,
                    b.PlatformSrc,b.PlatformName,b.SortNum AS PlatformSort,
                    c.GameSrc,c.GameName,c.GameIcon,c.SortNum AS GameSort, c.Remark AS GameRemark
                FROM {$this->table} a
                LEFT JOIN tblPlatform b ON b.PlatformID = a.PlatformID
                LEFT JOIN tblGame c ON c.GameID = a.GameID
                {$where}
                ORDER BY b.SortNum asc, c.GameSrc asc";

        return $this->getDbConnection()->getAll($sql, $args);
    }

    public function addMapByGame($id, $arr){
        $this->deleteAllByAttributes(array('GameID' => $id));
        if(!empty($arr)) $this->getDbConnection()->multiInsert($arr, 'tblPlatformGameMap', true);
    }

    public function addMapByPlatform($id, $arr){
        $this->deleteAllByAttributes(array('PlatformID' => $id));
        if(!empty($arr)) $this->getDbConnection()->multiInsert($arr, 'tblPlatformGameMap', true);
    }

    public static function getAllMap(){
        $modPlatformGameMap = ModPlatformGameMap::model();
        $gameList = array();
        foreach($modPlatformGameMap->select() as $v){
            $key = PubFunc::getGameKey($v['PlatformID'], $v['GameID']);
            $gameList[$key] = $v;
        }
        return $gameList;
    }
} 